package cn.com.sd.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import cn.com.sd.common.util.Page;
import cn.com.sd.system.dao.IOperatorDAO;
import cn.com.sd.system.model.Operator;
import cn.com.sd.system.service.IOperatorService;

@Component(value = "operatorService")
public class OperatorServiceImpl implements IOperatorService{

	private static final Logger logger = Logger.getLogger(OperatorServiceImpl.class);
	
	@Resource(name = "operatorDAO")
	private IOperatorDAO operatorDAO;
	
	@Override
	public Integer getOperatorCount() {
		
		int count = 0;
		try {
			count = operatorDAO.getOperatorCount();
			if (count <= 0) {
				count = 0;
			}
			logger.info("总数获取成功");
			
		} catch (Exception e) {
			count = 0;
			logger.info("总数获取失败" + e.getMessage());
		}
		
		return count;
	}
	
	@Override
	public List<Operator> list(Page page) throws Exception {
		
		List<Operator> operators = null;
		try {
			operators = operatorDAO.getOperatorList(page);
			logger.info("操作员列表获取成功");
			
		} catch (Exception e) {
			operators = null;
			logger.info("操作员列表获取失败");
		}
		
		return operators;
	}

	@Override
	public Operator validateLogin(Operator operator) {
		
		if (operator == null) {
			return null;
		}
		
		Operator result = null;
		try {
			result = operatorDAO.validateLogin(operator);
			if (result == null) {
				result = null;
			}
			logger.info("操作员验证是否成功" + result);
			
		} catch (Exception e) {
			result = null;
			logger.info("操作员验证失败");
		}
		
		return result;
	}

	@Override
	public Operator getDetail(Integer id) {
		
		if (id == null || id <= 0) {
			return null;
		}
		
		Operator operator = null;
		try {
			operator = operatorDAO.getObject(id);
			logger.info("操作员信息获取成功");
					
		} catch (Exception e) {
			logger.info("操作员信息获取失败" +e.getMessage());
		}
		return operator;
	}

	@Override
	public Boolean saveOrUpdate(Operator operator) throws Exception {
		
		if (operator == null) {
			return false;
		}
		
		boolean flag = false;
		try {
			operatorDAO.saveObject(operator);
			flag = true;
			logger.info("操作员信息保存成功");
			
		} catch (Exception e) {
			flag = false;
			logger.info("操作员信息保存失败" +e.getMessage());
		}
		return flag;
	}
	
	@Override
	public Boolean del(Integer id) {
		
		if (id == null || id <= 0) {
			return false;
		}
		
		boolean flag = false;
		try {
			Operator operator = operatorDAO.getObject(id);
			if (operator == null) {
				logger.info("操作员信息删除成功");
				return true;
			}
			operatorDAO.deleteObject(operator);
			flag = true;
			logger.info("操作员信息删除成功");
			
		} catch (Exception e) {
			flag = false;
			logger.info("操作员信息删除失败" +e.getMessage());
		}
		return flag;
	}

	@Override
	public Boolean delBanch(List<Integer> ids) {
		
		if (CollectionUtils.isEmpty(ids)) {
			return false;
		}
		
		boolean flag = false;
		try {
			List<Operator> operators = new ArrayList<Operator>();
			for (Integer id : ids) {
				if (id == null || id <= 0) {
					continue;
				}
				Operator temp = new Operator();
				temp.setId(id);
				operators.add(temp);
			}
			if (!CollectionUtils.isEmpty(operators)) {
				operatorDAO.deleteObjects(operators);
				logger.info("操作成功,删除总数为" + operators.size());
			} else {
				logger.info("操作员为空，操作成功");
			}
			flag = true;
			
		} catch (Exception e) {
			logger.info("操作员信息批量删除失败" +e.getMessage());
			flag = false;
		}
		return flag;
	}

}
